//
//  AppDelegate.m
//  01-UIView、UIWindow
//
//  Created by QC.L on 16/7/14.
//  Copyright © 2016年 QC.L. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()<UITextFieldDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 程序的组成结构
    // 1. main.m 主要实现了程序的正常运行
    // 2. AppDelegate 程序的执行者, 签订了UIAplicationDeledate(协议方法可选)
    // 注意: AppDelegate就是一个普通的类, 只不过签订了协议, 才有了这么多方法
    // 3. ViewContrller 视图控制器 主要是负责视图管理  看得见. 摸得到
    // 4. Main.sb(试图管理)   launchScreen.sb(负责启动页) 可视化管理(Xcode 7 以前 LaunchScreen.xib)
    // 5. Asset.xcassets 主要是用来管理图片素材的.
    // 6. info.plist (工程配置文件)
    
    
    // 模拟器
    // command + 1~5 改变模拟器大小
    // command + ←/→  改变模拟器的横竖屏
    
    
    // iOS概述
    // 正确地写法iOS
    
    
    //1. UIWindow(程序中, 只有一个主window)
    // 1> 取消程序的可视化入口, main.sb
    // 2> 创建主window 对象.
    // 创建 主window , 使其大小与屏幕一样
    // UIScreen  系统屏幕类
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    // 给你的窗口填颜色  UIColor  系统的颜色类
    _window.backgroundColor = [UIColor whiteColor];
    //3.  让window 显示, 成为主窗口
    [_window makeKeyAndVisible];
    //4. 设置window主窗口
    // Xcode 7 以后必须设置rootViewController. 否则, 在该方法结束时, 会崩溃
    _window.rootViewController = [[UIViewController alloc] init];
    
#if 0
    //3> UIWindow 继承于UIView
    
    // 2. UIView(与NSObject相似, 是UI阶段的根类, 大部分空间继承与UIView)
    //  学习新的类
    //    1>需要新类包含哪些初始化方法
    //    2>包含哪些行为(方法)和属性
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
    view.backgroundColor = [UIColor greenColor];
    [_window addSubview:view];
    
    
    //iOS系统坐标系
    // 1. 与数学中得坐标系不同, y的正方向是向下的
    // 2. 原点是屏幕的左上角.
    
    // frame  bounds  center
    // 1 . frame(x, y, width, height)  视图在其父视图上的位置
    // 2 . bounds(x, y, width, height)  视图本身的坐标系(0, 0, width, height)
    // bounds  width height修改idea本身的大小, 会以中心点扩张 或者缩小
    // bounds的改变, 不会改变该视图的位置
    // 3 . center 中心点
  //  view.frame = CGRectMake(10, 10, 80, 80);
    //bounds 的x, y 影响子视图的frame, 改变坐标系
    view.bounds = CGRectMake(10, 10, 80, 80);
    UIView *subview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    subview.backgroundColor = [UIColor yellowColor];
    [view addSubview:subview];

    //打印坐标
    NSLog(@"%@", NSStringFromCGRect(view.frame));
    //打印中心点
    NSLog(@"%@", NSStringFromCGPoint(view.center));
    
    // view.frame  get方法
    // frame.size   机构体
    // oc和c的内容结合, 是一个常量.
    
  //  view.frame.size.width = 100;
    CGRect rect = view.frame;
    rect.size.width = 100;
    view.frame = rect;
#endif
    
    
    // UIView 的基本属性
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    [_window addSubview:view];
    //移除
//    [view removeFromSuperview];
    // 1. 背景颜色
    view.backgroundColor = [UIColor cyanColor];
    // 2. alpha 透明度 0.0 ~ 1.0 默认值 1.0
    view.alpha = 0.2;
    // 3.tag  标记(编号)
    view.tag = 1000;
    // 根据tag获取到对应的view  __kingof
   UIView *view12 = [_window viewWithTag:1000];
    // 4.subviews
   UIView *arrayView = [_window.subviews objectAtIndex:0];
    
    // 5. superView
  //  view.superview;
    
    //6 . hidden  隐藏/ 显示
    view.hidden = YES;
    
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
    [_window addSubview:view1];
    view1.backgroundColor = [UIColor greenColor];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(20, 10, 50, 50)];
    [_window insertSubview:view2 atIndex:0];
    view2.backgroundColor = [UIColor redColor];
    
    UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(30, 10, 50, 50)];
    [_window addSubview:view3];
    view3.backgroundColor = [UIColor purpleColor];
    
    // 插入视图在指定下标
    //[_window insertSubview:<#(nonnull UIView *)#> atIndex:<#(NSInteger)#>];
    // 插入视图在指定视图上面
   // _window insertSubview:<#(nonnull UIView *)#> aboveSubview:<#(nonnull UIView *)#>
    
    
    // UILable(负责显示文本的)
    // 如果类没有自己的初始化方法 ,则使用父类的
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 400, 100)];
    lable.backgroundColor = [UIColor yellowColor];
    [_window addSubview:lable];
    
    
    // label的属性
    // 1. text
    lable.text = @"I'am wanghuang, wo hen huang. wo hen gou wo hen sanpao";
    // 2. textColor  文本颜色
    lable.textColor = [UIColor redColor];
    
    // 3. font  默认值17
    lable.font = [UIFont systemFontOfSize:20];
    lable.font = [UIFont boldSystemFontOfSize:40];
    
    // 4. textAligenment (对齐方式)(默认对齐方式是左)
    //   left  right   center
    lable.textAlignment = NSTextAlignmentRight;
    
    // 5. numberOfLines   默认为1行   不确定行数时 给0
    lable.numberOfLines = 1;
    
    // 6. lineBreakMode 换行模式,  ...的位置
    lable.lineBreakMode = NSLineBreakByTruncatingMiddle;
    
    // 7. 阴影 shadowColor
    lable.shadowColor = [UIColor blackColor];
    lable.shadowOffset = CGSizeMake(0, 2);
    
    // UIButton
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.backgroundColor = [UIColor purpleColor];
    [_window addSubview:button];
    button.frame = CGRectMake(150, 200, 100, 100);
    [button addTarget:self action:@selector(buttonClicke) forControlEvents:UIControlEventTouchUpInside];
    
    //给button添加文字
    [button setTitle:@"点我啊" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    
    // UItextField
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 200, 50)];
    textField.backgroundColor = [UIColor redColor];
    [_window addSubview:textField];
    textField.delegate = self;
    // 给输入框付了初值
    textField.text = @"文字";
    
    //borderstyle边缘样式
    textField.borderStyle = UITextBorderStyleLine;
    
    textField.placeholder = @"请输入您的王黄";
    textField.clearsOnBeginEditing = YES;
    
    // command   +    k 收起/显示 键盘
    
    
    // Override point for customization after application launch.
    return YES;
}


- (void)buttonClicke
{
    [_window endEditing:YES];
    NSLog(@"我被点了");
}

// 开始编辑前, 判断能否进行编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
}

// 已经开始编辑
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"11111111111");
}

// 已经结束编辑
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    
}

// 判断是否结束编辑-
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    // 处理点击return, 需要做的事
    // 回收键盘
    [textField endEditing:YES];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
